<?php
include('config.php');
header("content-type:text/html; charset=utf-8");
$where = 1;
$type_name = isset($_POST['type_name']) ? $_POST['type_name'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$number_name = isset($_POST['number_name']) ? $_POST['number_name'] : '';
$start_time = isset($_POST['start_time']) ? $_POST['start_time'] : '';
$end_time = isset($_POST['end_time']) ? $_POST['end_time'] : '';

$stime = strtotime($start_time);
$etime = strtotime($end_time)+3600*24-1;

//$where .= " and breed_name = '{$name}' and type_name = '{$type_name}' and number_name = '{$number_name}' and stock_goods_time>'{$stime}' and stock_goods_time<'{$etime}'";

if($start_time){
	$where = "s.stock_goods_time>='{$stime}'";
}
if($start_time && $end_time){
	$where = "s.stock_goods_time>='{$stime}' and s.stock_goods_time<='{$etime}'";
}
if($type_name){
	$type_id_rs = $mysql->selectlist("type","type_name='{$type_name}'");
	$type_id = $type_id_rs[0]['type_id'];
	$where = "s.type_id = '{$type_id}'";
}
if($type_name && $stime && $etime){
	$where = "s.type_id = '{$type_id}' and s.stock_goods_time>='{$stime}' and s.stock_goods_time<='{$etime}'";
}
if((($type_name&&$name) || $name) && $stime && $etime){
	$breed_id_rs = $mysql->selectlist("breed","breed_name='{$name}'");
	$breed_id = $breed_id_rs[0]['breed_id'];
	$where = "s.breed_id = '{$breed_id}' and s.stock_goods_time>='{$stime}' and s.stock_goods_time<='{$etime}'";
}
if((($type_name&&$name&&$number_name)||$number_name) && $stime && $etime){
	$number_id_rs = $mysql->selectlist("number","number_name='{$number_name}'");
	$number_id = $number_id_rs[0]['number_id'];
	$where = "s.number_id = '{$number_id}' and s.stock_goods_time>='{$stime}' and s.stock_goods_time<='{$etime}'";
}
if($type_name && $name && $number_name && $start_time){
	$where = "s.number_id = '{$number_id}' and s.stock_goods_time>='{$stime}'";
}
if($type_name && $name && $number_name && $start_time && $end_time){
	$where = "s.number_id = '{$number_id}' and s.stock_goods_time>='{$stime}' and s.stock_goods_time<='{$etime}'";
}
if($type_name && $name && $start_time && $end_time){
	$where = "s.breed_id = '{$breed_id}' and s.stock_goods_time>='{$stime}' and s.stock_goods_time<='{$etime}'";
}


$sql = "SELECT s.*,n.number_name,b.breed_name,t.type_name FROM stock_goods_log s left join `number` n on s.number_id=n.number_id left join breed b on s.breed_id=b.breed_id left join type t on s.type_id=t.type_id";
$sql .= " WHERE ".$where." ORDER BY s.stock_goods_id DESC LIMIT 0,15";
$n = 0;
echo "<table id='tables'>";
echo "<tr class='query_top_tr'>";
echo "<th>类别</th><th>品种</th><th>编号</th><th>数量</th><th>进货单价</th><th>进货总价</th><th>进货日期</th><th>操作</th>";
$rs = $mysql->execute($sql);
while($row = mysql_fetch_assoc($rs)){
	$n = $n;
	$num = $n%2;
	$total_price = $row['stock_goods_num'] * $row['stock_goods_price'];
	$time = date('Y-m-d',$row['stock_goods_time']);
	echo "<tr class='query_tr{$num}'><td>{$row['type_name']}</td><td>{$row['breed_name']}</td><td>{$row['number_name']}</td><td>{$row['stock_goods_num']}</td><td>{$row['stock_goods_price']}</td><td>{$total_price}</td><td>{$time}</td><td><span onclick='del_data({$row['stock_goods_id']})'>删除</span></td></tr>";
	$n++;
}
echo "</tr>";
echo "</table>";